package com.lanyun.serviceimpl;/**
 * @author zhy
 * @date 2021/1/6 - 12:05
 */

import com.lanyun.dao.RoleDao;
import com.lanyun.pojo.Role;
import com.lanyun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

 @ClassName RoleServiceImpl
 @Description xxx
 @Author zhy
 @Date 2021/1/6 - 12:05
 @Version 1.0
 **/
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Integer> getPrivilege(Integer rid) {
        return roleDao.findPrivilegeByRid(rid);//----根据角色id查询该角色的所有权限id
    }

    @Override
    public List<Role> getRole() {//查询所有角色
        return roleDao.getAllRole();
    }

    @Override
    public Integer insertRole(Role role) {//新增角色
        return roleDao.createRole(role);
    }

    @Override
    public Integer deleteRole(Integer id) {//根据角色id删除角色
        return roleDao.deleteRoleById(id);
    }

    @Override
    public Integer deleteSomeRole(String ids) {
        String id = ids.substring(0,ids.length()-1);
        String [] array = id.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            list.add(Integer.parseInt(array[i]));
        }
        return roleDao.deleteSomeRoleById(list);
    }

    @Override
    public Integer deletePrivilegeById(Integer rid) {//修改角色权限，先删除该角色的所有权限，在进行添加
        return roleDao.deleteAllPrivilegeById(rid);
    }

    @Override
    public Integer addPrivilege(Integer rid , Integer pid) {//删除该角色所有权限，然后进行增加权限
        return roleDao.addPrivilegeById(rid , pid);
    }
}
